package pkg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import jpa.Link;
import jpa.Page;

/**
 * Session Bean implementation class StatelessBean
 */
@Stateless
@EJB(name="StatelessBean", beanInterface=IStatelessBean.class)
public class StatelessBean implements IStatelessBean 
{

    @PersistenceContext
    EntityManager em;

    /**
     * Default constructor. 
     */
    public StatelessBean() 
    {
        // TODO Auto-generated constructor stub
    }

	@Override
	public Page getRootPage() {
		String query = "SELECT p FROM Page p WHERE p.pgType='ROOTMENU'";
		Query q = em.createQuery(query);
		Page page = (Page)q.getSingleResult();
		return page;
	}

	@Override
	public List<Link> sortLinks(Set<Link> s) {
		List<Link> list = new ArrayList<Link>();
		for(Link l : s) {
			list.add(l);
		}
		Collections.sort(list, new Comparator<Link>() {
			public int compare(Link l1, Link l2) {
				return l1.getLinkType().compareTo(l2.getLinkType());
			}
		});
		return list;
	}

	@Override
	public boolean isMenuPage(Page p) {
		if(getPageType(p) == PageType.MENU)
			return true;
		else
			return false;
	}

	@Override
	public boolean isImagePage(Page p) {
		if(getPageType(p) == PageType.IMAGE)
			return true;
		else
			return false;
	}

	@Override
	public boolean isVideoPage(Page p) {
		if(getPageType(p) == PageType.VIDEO)
			return true;
		else
			return false;
	}

	@Override
	public boolean isAppletPage(Page p) {
		if(getPageType(p) == PageType.APPLET)
			return true;
		else
			return false;
	}

	@Override
	public boolean isTextPage(Page p) {
		if(getPageType(p) == PageType.TEXT)
			return true;
		else
			return false;
	}

	@Override
	public PageType getPageType(Page p) {
		PageType ret = PageType.NONE;
		String type = p.getPgType();
		if(type.contains("MENU")) ret = PageType.MENU;
		else if(type.contains("TEXT")) ret = PageType.TEXT;
		else if(type.contains("IMAGE")) ret = PageType.IMAGE;
		else if(type.contains("VIDEO")) ret = PageType.VIDEO;
		else if(type.contains("APPLET")) ret = PageType.APPLET;
		return ret;
	}

	@Override
	public List<Link> getMenuLinks(Page p) {
		Set<Link> ret = new HashSet<Link>();
		if(isMenuPage(p))
		{
			for(Link l:p.getLinks2())
			{
				if(l.getLinkType().contains("MENU"))
					ret.add(l);
			}
		}
		return sortLinks(ret);
	}

	@Override
	public Link getNextLink(Page p) {
		Link ret = null;
		for(Link l:p.getLinks2())
		{
			if(l.getLinkType().contains("NEXT"))
			{
				ret = l;
				break;
			}
		}
		
		return ret;
	}

	@Override
	public Link getPreviousLink(Page p) {
		Link ret = null;
		for(Link l:p.getLinks1())
		{
			if(l.getLinkType().contains("NEXT"))
			{
				ret = l;
				break;
			}
		}
		
		return ret;
	}

	@Override
	public Link getUpLink(Page p) {
		Link ret = null;
		for(Link l:p.getLinks1())
		{
			if(l.getLinkType().contains("DOWN"))
			{
				ret = l;
				break;
			}
		}
		
		return ret;
	}

	@Override
	public Link getDownLink(Page p) {
		Link ret = null;
		for(Link l:p.getLinks2())
		{
			if(l.getLinkType().contains("DOWN"))
			{
				ret = l;
				break;
			}
		}
		
		return ret;
	}

	@Override
	public List<Link> getOtherLinks(Page p) {
		List<Link> ret = new ArrayList<Link>();
	
		for(Link l:p.getLinks2())
		{
			if(l.getLinkType().contains("OTHER"))
				ret.add(l);
		}
		
		return ret;
	}

	@Override
	public Page getPageById(long id) {
		String query = "SELECT p FROM Page p WHERE p.pgId=" + id;
		Query q = em.createQuery(query);
		Page page = (Page)q.getSingleResult();
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getDataFeedCategories()
	{
		System.out.println("Start getDataFeedCategories");
		ArrayList<String> ret = new ArrayList<String>();
		String query = "SELECT name FROM CATEGORY";
		
		//TODO createQuery Not working
		//jpa stuff needed?
		Query q = em.createQuery(query);
		System.out.println("Marker in getDataFeedCategories");
		ret = (ArrayList<String>) q.getResultList();
		
		return ret;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getDataFeedDataItems()
	{
		ArrayList<String> ret = new ArrayList<String>();
		String query = "SELECT name FROM DATAFEEDITEM";
		Query q = em.createQuery(query);
		ret = (ArrayList<String>) q.getResultList();
		
		return ret;
	}

	@Override
	public List<String> getDataFeedData(String dataType, int time)
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
